package cn.neusoft.service.impl;

import cn.neusoft.domain.Customer;
import cn.neusoft.mapper.CustomerMapper;
import cn.neusoft.query.BaseQuery;
import cn.neusoft.service.CustomerService;
import cn.neusoft.vo.PageBean;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class CustomerServiceImpl implements CustomerService {
  @Autowired
  CustomerMapper customerMapper;

  //新增用户
  @Transactional
  @Override
  public void add(Customer customer) {
    customerMapper.add(customer);
  }

  //修改用户
  @Transactional
  @Override
  public void update(Customer customer) {
    customerMapper.update(customer);
  }

  //伪删除用户（设del_flag为2）
  @Transactional
  @Override
  public void fakedelete(Customer customer) {
    customerMapper.fakedelete(customer);
  }


  //查询客户是否有过订单
  @Override
  public String findifOrder(String customer_id) {
      return customerMapper.findifOrder(customer_id);
  }

  //删除用户
  @Transactional
  @Override
  public void delete(String customer_id) {
    customerMapper.delete(customer_id);
  }

  //分页查询
  @Override
  public PageBean<Customer> findByPage(BaseQuery query) {
    //构建结果对象
    PageBean<Customer> pageBean = new PageBean<>();
    //开启分页
    PageHelper.startPage(query.getPageNow(),query.getPageSize());
    //分页查询得到当页的集合
    List<Customer> customerList = customerMapper.findByPage(query);
    //创建一个分页的实体PageInfo
    PageInfo<Customer> pageInfo = new PageInfo<>(customerList);

    pageBean.setTotalRows(pageInfo.getTotal());
    pageBean.setTotalPages(pageInfo.getPages());
    pageBean.setRowData(pageInfo.getList());

    return pageBean;
  }

  //分页查询
  @Override
  public PageBean<Customer> finddelByPage(BaseQuery query) {
    //构建结果对象
    PageBean<Customer> pageBean = new PageBean<>();
    //开启分页
    PageHelper.startPage(query.getPageNow(),query.getPageSize());
    //分页查询得到当页的集合
    List<Customer> customerList = customerMapper.finddelByPage(query);
    //创建一个分页的实体PageInfo
    PageInfo<Customer> pageInfo = new PageInfo<>(customerList);

    pageBean.setTotalRows(pageInfo.getTotal());
    pageBean.setTotalPages(pageInfo.getPages());
    pageBean.setRowData(pageInfo.getList());

    return pageBean;
  }
}
